# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

# 胞元几何形状参数
H = 10.0      # 胞元之间的间距
D = 4.0       # 圆直径
R = 0.5 * D   # 圆半径

# 阵列数目
X_N = 5
Y_N = 3

# 阵列间距
X_SPACE = H
Y_SPACE = 3 ** 0.5 * H

def sin(theta):
  return math.sin(theta / 180.0 * math.pi)

def cos(theta):
  return math.cos(theta / 180.0 * math.pi)

# 一点绕某一点旋转一定角度后的坐标
def rotatePoint(point, center, angle):
  x = (point[0] - center[0]) * cos(angle) - (point[1] - center[1]) * sin(angle) + center[0]
  y = (point[0] - center[0]) * sin(angle) + (point[1] - center[1]) * cos(angle) + center[1]
  return (x, y)

# 创建草图
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)

# 在坐标(x, y)点创建手性或反手性胞元
def createCellUnit(x, y, isAnti):
  # 绘制圆
  mdb.models['Model-1'].sketches['__profile__'].CircleByCenterPerimeter(center=(x, y), point1=(x+R, y))

  center = (x, y)

  theta = math.acos(R / (0.5 * H)) / math.pi * 180
  point_1 = (R * cos(theta) + x, R * sin(theta) + y)
  point_2 = (0.5 * H + x, 0 + y)
  
  # 第一条切线坐标
  if(isAnti == False):     # 手性胞元
    point_1_1 = point_1
    point_1_2 = point_2
  else:                    # 反手性胞元
    point_1_1 = (point_1[0], -point_1[1])
    point_1_2 = point_2
  
  # 通过旋转得到其他五条切线坐标
  point_2_1 = rotatePoint(point_1_1, center, 60)
  point_2_2 = rotatePoint(point_1_2, center, 60)

  point_3_1 = rotatePoint(point_1_1, center, 120)
  point_3_2 = rotatePoint(point_1_2, center, 120)

  point_4_1 = rotatePoint(point_1_1, center, 180)
  point_4_2 = rotatePoint(point_1_2, center, 180)

  point_5_1 = rotatePoint(point_1_1, center, 240)
  point_5_2 = rotatePoint(point_1_2, center, 240)

  point_6_1 = rotatePoint(point_1_1, center, 300)
  point_6_2 = rotatePoint(point_1_2, center, 300)
  
  # 绘制六条切线
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_1_1, point2=point_1_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_2_1, point2=point_2_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_3_1, point2=point_3_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_4_1, point2=point_4_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_5_1, point2=point_5_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_6_1, point2=point_6_2)

# 按规律排列胞元
def arrayCellUnit(start_x, start_y, x_n, x_space, y_n, y_space):

  for i in range(y_n):

    for j in range(x_n):

      x = j * x_space + start_x
      y = i * y_space + start_y
      
      createCellUnit(x, y, False)

print(" ")

# 打印参数
print("D: %s " % D)
print("H: %s " % H)
print("X_N: %d " % X_N)
print("Y_N: %d " % Y_N)

arrayCellUnit(0.0, 0.0, X_N, X_SPACE, Y_N, Y_SPACE)
arrayCellUnit(0.5*H, 0.5*3**0.5*H, X_N-1, X_SPACE, Y_N-1, Y_SPACE)

mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=
    DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Part-1'].BaseWire(sketch=
    mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
